<?php
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/cityList/ 城市列表获取
 * @apiName cityList
 * @apiGroup Community
 *
 * @apiParam {Number} city 城市id/获取省级城市为0(必填).
 * @apiParam {Number} level
 * -  1省/直辖市
 * -  2市
 * -  3区
 * -  4小区
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/communityList/ 城市小区列表获取
 * @apiName communityList
 * @apiGroup Community
 *
 * @apiParam {Number} cid 小区id(必填).
 * @apiParam {Number} level(必填)
 * -  5楼栋
 * -  6房号
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Community/authentication/ 注册户主
 * @apiName authentication
 * @apiGroup Community
 *
 * @apiParam {Number} uid 用户uid(必填).
 * @apiParam {String} idlist  1-2-3 用户选择的小区-楼栋-门牌号(必填).
 * @apiParam {String} name 用户名(必填).
 * @apiParam {Number} phone 用户电话号码(必填).
 * @apiParam {String} img 图片转码base64格式(非必填，存人脸时需要).
 * @apiParam {Number} usertype 用户类型(必填)
 * -  2业主
 * @apiParam {String} idcard 省份证号码(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/examineList/ 获取用户房产认证列表
 * @apiName examineList
 * @apiGroup Community
 *
 * @apiParam {Number} uid 用户uid(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 * @apiSuccess {NUmber} status  0认证中1认证通过2冻结3删除.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/residentsList/ 获取该房产下住客列表
 * @apiName residentsList
 * @apiGroup Community
 *
 * @apiParam {Number} aid 用户aid为获取用户房产认证列表id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 * @apiSuccess {NUmber} status  0认证中1认证通过2冻结3删除.
 * @apiSuccess {NUmber} data的key  1家属 2户主 3租客.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Community/residentsadd/ 注册住户
 * @apiName residentsadd
 * @apiGroup Community
 *
 * @apiParam {Number} fid 获取该房产下住客列表中id(必填).
 * @apiParam {String} name 用户名(必填).
 * @apiParam {Number} phone 用户电话号码(必填).
 * @apiParam {String} img 图片转码base64格式(非必填，存人脸时需要).
 * @apiParam {Number} uid 用户id(非必填，存人脸时需要).
 * @apiParam {Number} usertype 用户类型(必填)
 * -  1家属
 * -  3租客
 * @apiParam {String} idcard 省份证号码(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/searchUser/ 搜索访客用户列表
 * @apiName searchUser
 * @apiGroup Community
 *
 * @apiParam {String} condition 搜索条件 用户名/手机号码(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/inviteUser/ 访客邀请
 * @apiName inviteUser
 * @apiGroup Community
 *
 * @apiParam {Number} uid 被邀请人id(必填).
 * @apiParam {Number} aid 邀请户主(必填).
 * @apiParam {Number} loc_uid 邀请人id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/inviteshow/ 获取邀请页面数据
 * @apiName inviteshow
 * @apiGroup Community
 *
 * @apiParam {Number} id 邀请id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/inviteupdate/ 更改邀请状态
 * @apiName inviteupdate
 * @apiGroup Community
 *
 * @apiParam {Number} id 邀请id(必填).
 * @apiParam {Number} status 1接收 2拒绝(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/searchspace/ 搜索小区
 * @apiName searchspace
 * @apiGroup Community
 *
 * @apiParam {String} keyword 小区搜索关键字(必填).
 * @apiParam {Number} limit   页码(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/invitelist/ 被邀请人列表
 * @apiName invitelist
 * @apiGroup Community
 *
 * @apiParam {Number} uid   用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/residentsdel/ 删除住户
 * @apiName residentsdel
 * @apiGroup Community
 *
 * @apiParam {Number} did   批量删除各个id通过“,”串联住户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Community/invitedel/ 删除邀请
 * @apiName invitedel
 * @apiGroup Community
 *
 * @apiParam {Number} did   批量删除各个id通过“,”串联 邀请记录id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
namespace app\api\controller;
use app\common\controller\Api;
use think\Cache;
use \think\Log;
use think\Db;
use think\Model;
use think\Session;

class Community extends Api{
    //查找城市，小区等
    public function cityList(){
        //获取省市级 city 城市id 0为省级城市 level 为级别1为省级
        $result = $this->request->param();
        if ($result['city'] == 0 && $result['level'] == 1){
            $where['level'] = 1;
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] == 2){
            $where['level'] = 2;
            $where['parent_id'] = $result['city'];
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] == 3){
            $where['level'] = 3;
            $where['parent_id'] = $result['city'];
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] = 4){
            //获取小区
            $where['level'] = 4;
            $where['community_id'] = $result['city'];
            $data = M('Community')->where($where)->select();
        }else{
            $data = '';
        }
        if ($data){
            $return = codemsg(3);
            $return['data'] = $data;
        }else{
            $return = codemsg(-7000);
        }
        echo json_encode($return);
    }

    //获取小区列表
    public function communityList(){
        $result = $this->request->param();
        if ($result['cid'] > 0 && $result['level'] == 5){
            //获取小区的楼栋
            $where['parent_id'] = $result['cid'];
            $where['level'] = $result['level'];
            $data = M('community')->where($where)->select();
        }elseif ($result['cid'] > 0 && $result['level'] == 6){
            //获取小区的门户
            $where['parent_id'] = $result['cid'];
            $where['level'] = $result['level'];
            $data = M('community')->where($where)->select();
        }else{
            $data = '';
        }
        if ($data){
            $return = codemsg(3);
            $return['data'] = $data;
        }else{
            $return = codemsg(-7000);
        }

        echo json_encode($return);
    }

    //户主认证 uid(用户人脸存在不需要人脸照片) idlist 89-89-52 name phone usertype idcard
    public function authentication(){
//        if(!empty($_POST)){
        $result = $this->request->param();
//            $result['uid'] = 13;
//            $result['idlist'] = "1-2-3";
//            $result['name'] = '张建';
//            $result['phone'] = 15827024197;
//            $result['usertype'] = 2;
//            $result['idcard'] = '340826199410232258';
//            $face = new Face();
//            $result['img'] = $face->base64('http://imgbdb3.bendibao.com/shanghai/20152/4/201524103623754.jpg');
        Log::api('【Community/authentication】-[参数]--'.json_encode($result));
        //格式 55-88-44
        if (empty($result['idlist'])){
            //不存在该参数
            $return = codemsg(-1);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $id = explode('-',$result['idlist']);
        //小区id，查询是否存在小区
        if ($id[0] >0){
            $xwhere['id'] = $id[0];
            $xwhere['level'] = 4;
            $findcom = M('community')->where($xwhere)->find();
            if (empty($findcom)){
                //不存在该小区
                $return = codemsg(-7002);
                Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            //小区id错误
            $return = codemsg(-7002);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //楼栋id
        if ($id[1] >0){
            $lwhere['id'] = $id[1];
            $lwhere['level'] = 5;
            $findcoml = M('community')->where($lwhere)->find();
            if (empty($findcoml)){
                //不存在该楼栋
                $return = codemsg(-7003);
                Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            //楼栋id错误
            $return = codemsg(-7003);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //房号id
        if ($id[2] >0){
            $lwhere['id'] = $id[2];
            $lwhere['level'] = 6;
            $findcomf = M('community')->where($lwhere)->find();
            if (empty($findcomf) || $findcomf['uid'] > 0){
                //不存在该房号
                $return = codemsg(-7004);
                Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            //房号id错误
            $return = codemsg(-7004);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //姓名
        if (empty($result['name'])){
            //不能为空
            $return = codemsg(-7005);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //手机号码
        $phone = regcheck('phone',$result['phone']);
        if ($phone){
            //电话号码错误
            Log::api('【Community/authentication】-[返回]--'.json_encode($phone));
            echo json_encode($phone);die;
        }
        //身份证号
        $idcart = regcheck('idcard',$result['idcard']);
        if ($idcart){
            //身份证号码错误
            Log::api('【Community/authentication】-[返回]--'.json_encode($idcart));
            echo json_encode($idcart);die;
        }
        $usertype = array(
            1=>'家属',
            2=>'业主',
            3=>'租客',
            4=>'访客'
        );
        //身份
        if (empty($usertype[$result['usertype']])){
            //用户身份错误
            $return = codemsg(-7006);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['uid'] <=0 ){
            //用户信息错误
            $return = codemsg(-1);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //判断用户身份存在人脸
        if ($result['img']){
            $fwhere['uid'] = $result['uid'];
            $fwhere['phone'] = $result['phone'];
            $fwhere['status'] = 1;
            $findFace = M('face_openid')->where($fwhere)->find();
            if (empty($findFace)){
                //没有脸部信息存信息
                $face = new Face();
                $facedata['img'] = $result['img'];
                $facedata['userid'] = $result['uid'];
                $facedata['phone'] = $result['phone'];
                $addface = $face->Face('add',$facedata);
                if ($addface <= 0){
                    //新增失败
                    $return = codemsg(-7007);
                    Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                    echo json_encode($return);die;
                }
            }
        }
        //获取省市区地址
        $qid['id'] = array('eq',$findcom['community_id']);
        $q = M('region')->where($qid)->find();
        //市
        $sid['id'] = array('eq',$q['parent_id']);
        $s = M('region')->where($sid)->find();
        //省
        $slid['id'] = array('eq',$s['parent_id']);
        $sl = M('region')->where($slid)->find();
        Db::startTrans();
        try{
            //存用户信息
            $dataadd['username'] = $result['name'];
            $dataadd['phone'] = $result['phone'];
            $dataadd['card'] = $result['idcard'];
            $dataadd['uid'] = $result['uid'];
            //小区名
            $dataadd['xiaoqu'] = $findcom['name'];
            $dataadd['dz'] = $sl['name']." ".$s['name']." ".$q['name'];
            $dataadd['lou'] = $findcoml['name'].$findcomf['name'];
            $add = M('Auths')->insertGetId($dataadd);
            if ($add <=0){
                $return = codemsg(-7007);
                Log::api('【Community/authentication】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
            $swhere['id'] = $id[2];
            $swhere['level'] = 6;
            $map['uid'] = $add;
            $save = M('community')->where($swhere)->update($map);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            //记录错误日志
            Db::rollback();
            Log::api('【Community/authentication】-[返回]--'.json_encode($e));
            //返回错误
            $return = codemsg(-7007);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($save){
            $return = codemsg(3);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
//        }else{
//            //不是post提交
//            $return = codemsg(-7);
//            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
//            echo json_encode($return);die;
//        }
    }

    //审核列表
    public function examineList(){
        $result = $this->request->param();
//        $result['uid'] = 13;
        //获取uid
        if ($result['uid'] <=0 || empty($result['uid'])){
            $return = codemsg(-1);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询该用户下的房子
        $where['a.uid'] = array('eq',$result['uid']);
        $where['a.status'] = array('in',array(0,1,2));
        $datalist = M('auths')
            ->alias('a')
            ->field('a.*,c.id as cid')
            ->join('tp_community c','c.uid=a.id')
            ->where($where)
            ->select();
        if ($datalist){
            $return = codemsg(5003);
            $return['data'] = $datalist;
            Log::api('【Community/examineList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7022);
            Log::api('【Community/examineList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //户主/家人/租客列表
    public function residentsList(){
        $result = $this->request->param();
//            $result['aid'] = 21;
        //获取查看户主表id是否正确
        if ($result['aid'] <=0 || empty($result['aid'])){
            $return = codemsg(-1);
            Log::api('【Community/residentsList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //获取户主信息
        $where['id'] = array('eq',$result['aid']);
        $datalist = M('auths')
            ->where($where)
            ->find();
        if ($datalist){
            $datalist['rname'] = $datalist['username'];
            $rlist[2] = $datalist;
        }else{
            $rlist[2] = array();
        }

        $rlist[1] = array();
        $rlist[3] = array();
        //获取租客/家属等信息
        $fwhere['fid'] = array('eq',$result['aid']);
        $fwhere['is_del'] = array('eq',0);
        $rdata = M('residents')
            ->where($fwhere)
            ->select();
        if ($rdata){
            foreach ($rdata as $k=>$v){
                if ($v['status']==1){
                    $rlist[1][] = $v;
                }else{
                    $rlist[3][] = $v;
                }
            }
        }
        if ($rlist){
            $return = codemsg(5003);
            $return['data'] = $rlist;
            Log::api('【Community/residentsList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7022);
            Log::api('【Community/residentsList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //添加住户
    public function residentsadd(){
//        if(!empty($_POST)){
        $result = $this->request->param();
//        $result['fid'] = 21;
//        $result['name'] = '李四';
//        $result['phone'] = 15827024188;
//        $result['usertype'] = 1;
//        $result['idcard'] = '340826199410232258';
//        $face = new Face();
//        $result['img'] = $face->base64('http://5b0988e595225.cdn.sohucs.com/images/20180113/6bcabfb4fadc42ec87817a128a568a10.png');
        Log::api('【Community/residentsadd】-[参数]--'.json_encode($result));

        //姓名
        if (empty($result['name'])){
            //不能为空
            $return = codemsg(-7005);
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //手机号码
        $phone = regcheck('phone',$result['phone']);
        if ($phone){
            //电话号码错误
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($phone));
            echo json_encode($phone);die;
        }
        //身份证号
        $idcart = regcheck('idcard',$result['idcard']);
        if ($idcart){
            //身份证号码错误
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($idcart));
            echo json_encode($idcart);die;
        }
        //身份
        if ($result['usertype'] !=1 && $result['usertype'] !=3){
            //用户身份错误
            $return = codemsg(-7006);
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //判断fid是否存在
        if ($result['fid'] <= 0 && empty($result['fid'])){
            //用户身份错误
            $return = codemsg(-7006);
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $findwhere['id'] = $result['fid'];
        $findwhere['status'] = 1;
        $findauth = M('auths')->where($findwhere)->find();
//        dump($findauth);die;
        if (empty($findauth)){
            $return = codemsg(-7008);
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //判断用户身份存在人脸
        if ($result['img']){
            $fwhere['uid'] = $findauth['uid'];
            $fwhere['phone'] = $result['phone'];
            $fwhere['status'] = 1;
            $findFace = M('face_openid')->where($fwhere)->find();
            if (empty($findFace)){
                //没有脸部信息存信息
                $face = new Face();
                $facedata['img'] = $result['img'];
                $facedata['userid'] = $fwhere['uid'];
                $facedata['phone'] = $result['phone'];
                $addface = $face->Face('add',$facedata);
                if ($addface <= 0){
                    //新增失败
                    $return = codemsg(-7007);
                    Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
                    echo json_encode($return);die;
                }
            }
        }
        //是否存在该用户
        $userwhere['phone'] = $result['phone'];
        $userwhere['fid'] = $result['fid'];
        $userwhere['is_del'] = 0;
        $finduser = M('residents')->where($userwhere)->find();
        if ($finduser){
            $return = codemsg(-7009);
            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //存用户信息
        $dataadd['rname'] = $result['name'];
        $dataadd['phone'] = $result['phone'];
        $dataadd['card'] = $result['idcard'];
        $dataadd['fid'] = $result['fid'];
        $dataadd['status'] = $result['usertype'];
        $save = M('residents')->insertGetId($dataadd);
        if ($save){
            $return = codemsg(3);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

//        }else{
//            //不是post提交
//            $return = codemsg(-7);
//            Log::api('【Community/residentsadd】-[返回]--'.json_encode($return));
//            echo json_encode($return);die;
//        }
    }

    //住户删除
    public function residentsdel(){
        $result = $this->request->param();
        Log::api('【Community/residentsdel】-[参数]--'.json_encode($result));
        if (empty($result['did'])){
            //参数判断
            $return = codemsg(-1);
            Log::api('【Community/residentsdel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $where['id'] = array('in',$result['did']);
        $map['is_del'] = 1;
        $data = M('residents')->where($where)->update($map);
        if ($data){
            $return = codemsg(3);
            Log::api('【Community/residentsdel】-[返回]--'.json_encode($return));
            echo json_encode($return);
        }else{
            $return = codemsg(-7017);
            Log::api('【Community/residentsdel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

    }

    //访客记录删除
    public function invitedel(){
        $result = $this->request->param();
        Log::api('【Community/invitedel】-[参数]--'.json_encode($result));
        if (empty($result['did'])){
            //参数判断
            $return = codemsg(-1);
            Log::api('【Community/invitedel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $where['id'] = array('in',$result['did']);
        $map['is_del'] = 1;
        $data = M('visitors')->where($where)->update($map);
        if ($data){
            $return = codemsg(3);
            Log::api('【Community/authentication】-[返回]--'.json_encode($return));
            echo json_encode($return);
        }else{
            $return = codemsg(-7017);
            Log::api('【Community/invitedel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

    }

    //访客搜索
    public function searchUser(){
        $result = $this->request->param();
        Log::api('【Community/searchUser】-[参数]--'.json_encode($result));
        if (empty($result['condition'])){
            //参数判断
            $return = codemsg(-1);
            Log::api('【Community/searchUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //搜索用户
        $where['is_lock'] = array('eq',0);
        $where['mobile|nickname'] = array('like','%'.$result['condition'].'%');
        $userlist = M('users')->field('mobile,nickname,user_id')->where($where)->select();
        if ($userlist){
            $return = codemsg(5003);
            $return['data'] = $userlist;
            Log::api('【Community/searchUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7010);
            Log::api('【Community/searchUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //小区搜索
    public function searchspace(){
        $result = $this->request->param();
        if (empty($result['keyword'])){
            $return = codemsg(-7000);
            Log::api('【Community/searchspace】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['limit'] <=0 || empty($result['limit'])){
            $result['limit'] = 1;
        }
        $star = ($result['limit']-1)*20;
        $end = $result['limit']*20;
        //搜索区级地址
        $citywhere['level'] = array('eq',3);
        $gitcity = Cache::get('searchspace_city_1');
        if (empty($gitcity)){
            $city = M('Region')->where($citywhere)->column('name','id');
            $gitcity = Cache::set('searchspace_city_1',$city,100000);
        }
        $where['name'] = array('like','%'.$result['keyword'].'%');
        $where['level'] = array('eq',4);
        $xioqu = M('community')->where($where)->limit($star,$end)->select();
        $data = array();
        if ($xioqu){
            foreach ($xioqu as $k=>$v){
                $data[$k] = $v;
                $data[$k]['city'] = $gitcity[$v['community_id']];
            }
        }
        $return = codemsg(5003);
        $return['data'] = $data;
        Log::api('【Community/searchspace】-[返回]--'.json_encode($return));
        echo json_encode($return);die;
    }

    //访客邀请 uid被邀请人  aid 邀请的户主 loc_uid
    public function inviteUser(){
        $result = $this->request->param();
        //查询是否今天已经邀请
        $wherevi['loc_uid'] = array('eq',$result['loc_uid']);
        $wherevi['uid'] = array('eq',$result['uid']);
        $wherevi['status'] = array('eq',0);
        $wherevi['time'] = date('Y-m-d',time());
        $find = M('visitors')->where($wherevi)->find();
        if ($find){
            //参数判断
            $return = codemsg(-7013);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        Log::api('【Community/inviteUser】-[参数]--'.json_encode($result));
        if (!$result['uid'] ||!$result['loc_uid'] || !$result['aid']){
            //参数判断
            $return = codemsg(-1);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //搜索uid用户
        $whereuid['user_id'] = array('eq',$result['uid']);
        $whereuid['is_lock'] = array('eq',0);
        $userlist = M('users')->where($whereuid)->find();
        if (empty($userlist)){
            $return = codemsg(-7011);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //搜索loc_uid用户
        $wherelocuid['user_id'] = array('eq',$result['loc_uid']);
        $wherelocuid['is_lock'] = array('eq',0);
        $locuserlist = M('users')->where($wherelocuid)->find();
        if (empty($locuserlist)){
            $return = codemsg(-7011);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //
        //搜索aid户主是否存在
        $where['a.id'] = array('eq',$result['aid']);
        $where['a.status'] = array('eq',1);
        $datalist[0] = M('auths')
            ->alias('a')
            ->join('tp_community c','c.uid=a.id')
            ->where($where)
            ->find();
        if (empty($datalist)){
            $return = codemsg(-7020);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if (empty($userlist)){
            $return = codemsg(-7012);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

        $map['phone'] = $locuserlist['number'];
        $map['uid'] = $userlist['user_id'];
        if (empty($locuserlist['head_pic'])){
            $map['pic'] = '/public/upload/pic/default.jpg';
        }else{
            $map['pic'] = $locuserlist['head_pic'];
        }
        $map['name'] = $userlist['nickname'];
        $map['aname'] = $locuserlist['nickname'];
        $map['aid'] = $result['aid'];
        $map['loc_uid'] = $locuserlist['user_id'];
        $map['address'] = $datalist['0']['dz'].$datalist['0']['xiaoqu'].$datalist['0']['lou'];
        $map['invitation_id'] = $locuserlist['user_id'];
        $map['time'] = date('Y-m-d',time());
        $add = M('visitors')->insertGetId($map);
        if ($add){
            //邀请成功后推送
            $wherejg['uid'] = $map['uid'];
            $jg = M('jgid')->where($wherejg)->find();
            $jgid = $jg['ppid'];
            if ($userlist['head_pic']){
                $jushdata['pic'] = $userlist['head_pic'];
            }else{
                $jushdata['pic'] = '/public/upload/pic/default.jpg';
            }
            $jushdata['xiaoqu'] = $map['address'];
            $jushdata['username'] = $map['name'];
            $jushdata['invite_id'] = $add;
            $jushdata['title'] = '尊敬的智慧小区用户，您有一条邀请记录！';
            $jushdata['type'] = 1;//邀请
            sendjg($jushdata,$jgid);
            //添加邀请
            $return = codemsg(3);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

    }

    //获取邀请页面数据
    public function inviteshow(){
        $result = $this->request->param();
        if (empty($result['id']) || $result['id'] <= 0){
            $return = codemsg(-1);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询该记录
        $where['id'] = $result['id'];
        $find = M('visitors')->where($where)->find();
        if ($find['status'] == 1){
            $return = codemsg(-7014);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($find['status'] == 2){
            $return = codemsg(-7015);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $return = codemsg(3);
        $return['data'] = $find;
        Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
        echo json_encode($return);die;
    }

    //邀请人列表
    public function invitelist(){
        $result = $this->request->param();
        if (empty($result['uid']) ||$result['uid'] <= 0){
            $return = codemsg(-1);
            Log::api('【Community/invitelist】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //获取列表
        $where['uid'] = $result['uid'];
        $where['is_del'] = 0;
        $datalist = M('visitors')
            ->field('id as iid,uid,phone,pic,name,aid,address,status,invitation_id,aname,time')
            ->where($where)
            ->select();
        $return = codemsg(3);
        $return['data'] = $datalist;
        Log::api('【Community/invitelist】-[返回]--'.json_encode($return));
        echo json_encode($return);die;
    }

    //更改邀请状态
    public function inviteupdate(){
        $result = $this->request->param();
        Log::api('【Community/inviteupdate】-[参数]--'.json_encode($result));
        if (empty($result['id']) || $result['id'] <= 0){
            $return = codemsg(-1);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['status'] !=1 && $result['status'] !=2){
            $return = codemsg(-1);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //查询该记录
        $where['id'] = $result['id'];
        $find = M('visitors')->where($where)->find();
        if ($find['status'] == 1){
            $return = codemsg(-7014);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($find['status'] == 2){
            $return = codemsg(-7015);
            Log::api('【Community/inviteshow】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }

        //更改状态
        $map['status'] = $result['status'];
        $save = M('visitors')->where($where)->update($map);
        if ($save){
            $return = codemsg(3);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7007);
            Log::api('【Community/inviteUser】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }
}